﻿package com.vicentediaz.view.imageviewer
{
    import com.greensock.*;
    import com.myp.flow.*;
    import com.myp.mystage.*;
    import com.vicentediaz.data.*;
    import com.vicentediaz.flow.*;
    import com.vicentediaz.view.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;

    public class ImageViewer extends Sprite
    {
        private var originalHeight:Number;
        private var image:Loader;
        public var currentImageId:uint;
        private var nextImage:String;
        private var imageFlow:Flow;
        private var zDepthImage:ZDepthImage;
        private var originalWidth:Number;
        private var zDepth:Loader;
        public static var verbose:Boolean = true;
        private static var instance:ImageViewer;

        public function ImageViewer()
        {
            this.imageFlow = Flow.level(ImageFlow.LEVEL);
            this.imageFlow.addEventListener(FlowEvent.REQUEST, this.onImageFlowRequest, false, 0, true);
            this.imageFlow.addEventListener(FlowEvent.CHANGE, this.onImageFlowChange, false, 0, true);
            addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage, false, 0, true);
            return;
        }// end function

        private function onResize(param1:Event) : void
        {
            var _loc_2:* = MyStage.getInstance();
            if (this.image.content)
            {
                this.image.width = _loc_2.width;
                this.image.scaleY = this.image.scaleX;
                if (this.image.height < _loc_2.height)
                {
                    this.image.height = _loc_2.height;
                    this.image.scaleX = this.image.scaleY;
                }// end if
                this.image.x = _loc_2.centerX - this.image.width * 0.5;
                this.image.y = _loc_2.centerY - this.image.height * 0.5;
                this.zDepthImage.scaleX = this.image.scaleX * 1.2;
                this.zDepthImage.scaleY = this.image.scaleY * 1.2;
                this.zDepthImage.x = _loc_2.centerX - this.image.width * 1.2 * 0.5;
                this.zDepthImage.y = _loc_2.centerY - this.image.height * 1.2 * 0.5;
            }// end if
            return;
        }// end function

        private function onZDepthLoadingComplete(param1:Event) : void
        {
            var _loc_2:* = Bitmap(this.image.content);
            var _loc_3:* = Bitmap(this.zDepth.content);
            this.image.scaleX = 1;
            this.image.scaleY = 1;
            this.zDepthImage.viewport.viewportWidth = this.image.width;
            this.zDepthImage.viewport.viewportHeight = this.image.height;
            this.zDepthImage.build(_loc_3.bitmapData, _loc_2.bitmapData);
            addChild(this.zDepthImage);
            TweenMax.fromTo(this.zDepthImage, 2, {alpha:0}, {alpha:1});
            this.say("ZDepth cargado.");
            this.onResize(null);
            return;
        }// end function

        private function onImageFlowChange(param1:FlowEvent) : void
        {
            if (this.imageFlow.currentSection != ImageFlow.NONE)
            {
                this.load(int(param1.currentSection));
            }// end if
            return;
        }// end function

        private function onImageLoaded(param1:Event) : void
        {
            var _loc_2:* = new ImageViewerEvent(ImageViewerEvent.IMAGE_LOADED);
            _loc_2.loaderInfo = this.image.contentLoaderInfo;
            _loc_2.id = this.currentImageId;
            dispatchEvent(_loc_2);
            var _loc_3:* = Bitmap(this.image.content);
            _loc_3.smoothing = true;
            this.say("carga finalizada. Cargando ZDepth.");
            this.zDepth.load(new URLRequest(Config.getInstance().getByValue("IMAGES_ZDEPTH_PATH") + this.nextImage));
            this.zDepth.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onZDepthLoadingComplete, false, 0, true);
            return;
        }// end function

        private function onAddedToStage(param1:Event) : void
        {
            MyStage.getInstance().stage.addEventListener(Event.RESIZE, this.onResize, false, 0, true);
            return;
        }// end function

        private function onImageFlowRequest(param1:FlowEvent) : void
        {
            this.imageFlow.accept("ImageViewer");
            return;
        }// end function

        public function load(param1:uint) : void
        {
            this.say("cargando #" + param1 + "   >   " + DAO.getInstance().gallery.image[param1].toString());
            this.nextImage = DAO.getInstance().gallery.image[param1].toString();
            this.currentImageId = param1;
            if (!this.image)
            {
                this.image = new Loader();
                this.zDepth = new Loader();
                this.zDepthImage = ZDepthImage.getInstance();
                TweenMax.to(this.zDepthImage, 0, {alpha:0});
                this.startLoad();
            }
            else
            {
                TweenMax.to(this.zDepthImage, 0.5, {alpha:0, onComplete:this.startLoad});
            }// end else if
            return;
        }// end function

        private function say(param1:String) : void
        {
            if (verbose)
            {
                trace("<< IMAGE VIEWER : " + param1 + " >>");
            }// end if
            return;
        }// end function

        private function startLoad() : void
        {
            this.image.unload();
            this.zDepth.unload();
            if (MyStage.getInstance().width > int(Config.getInstance().getByValue("IMAGES_HQ_LIMIT")))
            {
                this.image.load(new URLRequest(Config.getInstance().getByValue("IMAGES_HQ_PATH") + this.nextImage));
            }
            else
            {
                this.image.load(new URLRequest(Config.getInstance().getByValue("IMAGES_PATH") + this.nextImage));
            }// end else if
            this.image.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onImageLoaded, false, 0, true);
            this.image.contentLoaderInfo.addEventListener(Event.OPEN, this.onStartLoading, false, 0, true);
            this.image.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.onLoadingProgress, false, 0, true);
            return;
        }// end function

        private function onLoadingProgress(param1:ProgressEvent) : void
        {
            var _loc_2:* = new ImageViewerEvent(ImageViewerEvent.IMAGE_LOADING_PROGRESS);
            _loc_2.loaderInfo = this.image.contentLoaderInfo;
            _loc_2.id = this.currentImageId;
            _loc_2.progress = param1.bytesLoaded / param1.bytesTotal;
            dispatchEvent(_loc_2);
            return;
        }// end function

        private function onStartLoading(param1:Event) : void
        {
            var _loc_2:* = new ImageViewerEvent(ImageViewerEvent.IMAGE_START_LOADING);
            _loc_2.loaderInfo = this.image.contentLoaderInfo;
            _loc_2.id = this.currentImageId;
            dispatchEvent(_loc_2);
            return;
        }// end function

        public static function getInstance() : ImageViewer
        {
            instance = instance ? (instance) : (new ImageViewer);
            return instance;
        }// end function

    }
}
